<template>
	<view class="container">
		<c-navBar title="账号信息" isPerch></c-navBar>
		<view class="content">

			<view @click="uploadAvatar" class="item">
				<text class="text">头像</text>
				<view class="item-right">
					<image class="head" v-if="form.avatar" :src="vuex_imgUrl + form.avatar" mode="aspectFill" />
					<image class="head" v-else src="/static/home/avator1.png" mode="aspectFill" />
					<u-icon name="arrow-right" color="#666666" size="20rpx"></u-icon>
				</view>
			</view>

			<view class="items item">
				<text class="text">联系人</text>
				<view class="item-right">
					<input class="ipt" v-model="form.nickName" placeholder="请输入昵称" />
				</view>
			</view>

			<view class="items item">
				<text class="text">电话</text>
				<view class="item-right">
					<input type="number" class="ipt" v-model="form.contactPhone" placeholder="请输入手机号" />
				</view>
			</view>


			<view class="btn-box">
				<view class="btn" @click="submit">保存</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				height: this.$titleHeight,
				form: {
					avatar: ''
				},
			};
		},
		methods: {
			// 上传头像
			uploadAvatar() {
				uni.chooseImage({
					count: 1, //默认9
					success: (res) => {
						uni.uploadFile({
							url: uni.$u.http.config.baseURL + '/common/upload',
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (uploadFileRes) => {
								console.log(uploadFileRes)
								let imgData = JSON.parse(uploadFileRes.data)
								console.log(imgData)
								if (imgData.code == 200) {
									uni.showToast({
										mask: true,
										icon: 'none',
										title: '上传成功'
									})
									this.form.avatar = imgData.fileName
								} else {
									uni.showToast({
										mask: true,
										icon: 'none',
										title: res.msg
									})
								}
							}
						});
					},
					fail: (res) => {
						uni.showToast({
							mask: true,
							icon: 'none',
							title: res.msg
						})
					}
				});
			},
			/** 保存 */
			async submit() {

				if (this.form.nickName.length > 6) {
					return uni.showToast({
						icon: 'none',
						title: '联系人昵称最多6个字'
					})
				}

				if (!this.$fn.phoVerify(this.form.contactPhone)) return

				console.log(this.form);
				const res = await this.$api.update(this.form)
				if (res.code == 200) {
					uni.showToast({
						title: '保存成功',
						icon: 'none'
					})
					uni.navigateBack()
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			/** 获取个人信息 */
			async getUserInfo() {
				const res = await this.$api.getInfo()
				if (res.code == 200) {
					console.log("userInfo", res);

					this.form = res.data
					console.log(this.form)
				} else {
					return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			}
		},
		onShow() {
			this.getUserInfo()
		}
	};
</script>


<style scoped lang="scss">
	.container {
		.content {
			padding: 0 32rpx;

			.item {
				display: flex;
				justify-content: space-between;
				border-bottom: 2rpx solid #EEEEEE;
				height: 140rpx;
				align-items: center;

			}

			.item1 {
				display: block;
				height: 450rpx;
				margin-top: 40rpx;
			}

			.items {
				height: 110rpx;
			}

			.item-right {
				flex: 1;
				display: flex;
				justify-content: flex-end;
				align-items: center;
			}

			.item-right1 {
				margin-top: 40rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.img {
					width: 332rpx;
					height: 216rpx;
					border-radius: 0rpx 0rpx 0rpx 0rpx;
				}
			}

		}
	}



	.head {
		// width: 90rpx;
		// height: 90rpx;
		// border-radius: 45rpx;
		margin-right: 10rpx;


		background: #eee;
		width: 80rpx;
		height: 80rpx;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
	}

	.item {
		display: flex;
		justify-content: space-between;
		border-bottom: 2rpx solid #EEEEEE;
		height: 140rpx;
		align-items: center;

	}

	.items {
		height: 110rpx;
	}

	.item-right {
		flex: 1;
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}

	.text {
		font-weight: 500;
		font-size: 32rpx;
		color: #1A1A1A;
	}

	.ipt {
		width: 500rpx !important;
		font-size: 24rpx;
		color: #999999;
		margin-right: 8rpx;
		text-align: right;
	}

	.ipt1 {
		width: 500rpx;
	}

	::v-deep .u-textarea {
		background-color: #F7F7F7 !important;
	}

	.footer {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 0 40rpx;
		--iphonex-fix-bottom: env(safe-area-inset-bottom);
		padding-bottom: var(--iphonex-fix-bottom);

		view {
			width: 670rpx;
			height: 96rpx;
			line-height: 96rpx;
			text-align: center;
			color: #fff;
			font-size: 36rpx;
			font-weight: bold;
			background: $c-bgColor;
			border-radius: 16rpx;
		}
	}
</style>